package yu.liu.personal.app.myapplication.components.views;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yu.liu.personal.app.myapplication.R;
import yu.liu.personal.app.myapplication.components.PopupCollection;
import yu.liu.personal.app.myapplication.components.items.ComparePanelItem;
import yu.liu.personal.app.myapplication.models.AVGCompareItem;
import yu.liu.personal.app.myapplication.models.AVGItem;

/**
 * Created by yu.liu on 2016/9/30.
 */

public class ComparePanelPopupWindow extends PopupCollection {

    @BindView(R.id.editLevelCompPop)
    EditText editLevelCompPop;

    @BindView(R.id.editEnhanceCompPop)
    EditText editEnhanceCompPop;

    @BindView(R.id.cutLineLast)
    View cutLineLast;

    @BindView(R.id.btnRunCompPop)
    View btnRunCompPop;

    @BindView(R.id.btnCancelCompPop)
    View btnCancelCompPop;

    private List<AVGCompareItem> compareItems;
    private List<AVGItem> originItems;

    private ComparePanelItem panelItem_1;
    private ComparePanelItem panelItem_2;
    private ComparePanelItem panelItem_3;

    private int level = 1;
    private int enhance = 0;

    public ComparePanelPopupWindow(Context context) {
        super(context);
    }

    public void setCompareItems(List<AVGItem> items) {
        if (items != null) {
            originItems = items;
            if (compareItems == null) {
                compareItems = new ArrayList<>();
            } else {
                compareItems.clear();
            }
            for (int i = 0; i < items.size(); i++) {
                AVGItem it = items.get(i);
                AVGCompareItem item = new AVGCompareItem();
                item.name = it.name;
                item.type = it.type;
                item.stars = it.stars;
                item.flagHP = null;
                item.flagAmor = null;
                item.flagShield = null;
                item.flagResist = null;
                item.flagAtk = null;
                item.flagScout = null;
                item.flagSpd = null;
                item.flagOil = null;
                item.flagAmmo = null;
                compareItems.add(item);
            }
        }
    }

    @Override
    public boolean showPopupWindow() {
        if(anchorView != null && windowView != null) {
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setContentView(windowView);
//            showAsDropDown(anchorView);
            showAtLocation(anchorView, Gravity.NO_GRAVITY, 0, 0);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void loadViews() {
        ButterKnife.bind(this, windowView);
        panelItem_1 = new ComparePanelItem(resContext);
        panelItem_2 = new ComparePanelItem(resContext);
        panelItem_3 = new ComparePanelItem(resContext);
        panelItem_1.initView(windowView, resManager.getResourceID("id",
                "boxItemCompPop_1"));
        panelItem_2.initView(windowView, resManager.getResourceID("id",
                "boxItemCompPop_2"));
        panelItem_3.initView(windowView, resManager.getResourceID("id",
                "boxItemCompPop_3"));
    }

    @Override
    public void loadMembers() {

    }

    @Override
    public void postSetup() {

    }

    @Override
    public void updateViews() {
        if (compareItems != null) {
            if (compareItems.size() == 2) {
                panelItem_3.getContentView().setVisibility(View.GONE);
                cutLineLast.setVisibility(View.GONE);
                calculateTheItem(0);
                calculateTheItem(1);
                compare(compareItems.get(0), compareItems.get(1));
                panelItem_1.setItem(compareItems.get(0));
                panelItem_2.setItem(compareItems.get(1));
                panelItem_1.updateViews();
                panelItem_2.updateViews();
            } else if(compareItems.size() == 3) {
                calculateTheItem(0);
                calculateTheItem(1);
                calculateTheItem(2);
                compare(compareItems.get(0), compareItems.get(1), compareItems.get(2));
                panelItem_1.setItem(compareItems.get(0));
                panelItem_2.setItem(compareItems.get(1));
                panelItem_3.setItem(compareItems.get(2));
                panelItem_1.updateViews();
                panelItem_2.updateViews();
                panelItem_3.updateViews();
            }
        }
    }

    private void calculateTheItem(int index) {
        AVGCompareItem item = compareItems.get(index);
        AVGItem it = originItems.get(index);
        item.hp = (int) Math.ceil(((level + enhance + 11) / 12f) * it.hp);
        item.armor = (int) Math.ceil(((level + enhance + 11) / 12f) * it.armor);
        item.shield = (int) Math.ceil(((level + enhance + 11) / 12f) * it.shield);
        item.resist = (int) Math.ceil(((level + enhance + 11) / 12f) * it.resist);
        item.atk = (int) Math.ceil(((level + enhance + 11) / 12f) * it.atk);
        item.scout = (int) Math.ceil(((level + enhance + 11) / 12f) * it.scout);
        item.spd = (int) Math.ceil(((level + enhance + 11) / 12f) * it.spd);
        item.oilConsume = it.oilConsume;
        item.ammoConsume = it.ammoConsume;
    }

    private void compare(AVGCompareItem first, AVGCompareItem second) {
        if (first.hp > second.hp) {
            first.flagHP = true;
            second.flagHP = false;
        } else if (first.hp < second.hp) {
            first.flagHP = false;
            second.flagHP = true;
        } else {
            first.flagHP = null;
            second.flagHP = null;
        }

        if (first.armor > second.armor) {
            first.flagAmor = true;
            second.flagAmor = false;
        } else if (first.armor < second.armor) {
            first.flagAmor = false;
            second.flagAmor = true;
        } else {
            first.flagAmor = null;
            second.flagAmor = null;
        }

        if (first.shield > second.shield) {
            first.flagShield = true;
            second.flagShield = false;
        } else if (first.shield < second.shield) {
            first.flagShield = false;
            second.flagShield = true;
        } else {
            first.flagShield = null;
            second.flagShield = null;
        }

        if (first.resist > second.resist) {
            first.flagResist = true;
            second.flagResist = false;
        } else if (first.resist < second.resist) {
            first.flagResist = false;
            second.flagResist = true;
        } else {
            first.flagResist = null;
            second.flagResist = null;
        }

        if (first.atk > second.atk) {
            first.flagAtk = true;
            second.flagAtk = false;
        } else if (first.atk < second.atk) {
            first.flagAtk = false;
            second.flagAtk = true;
        } else {
            first.flagAtk = null;
            second.flagAtk = null;
        }

        if (first.scout > second.scout) {
            first.flagScout = true;
            second.flagScout = false;
        } else if (first.scout < second.scout) {
            first.flagScout = false;
            second.flagScout = true;
        } else {
            first.flagScout = null;
            second.flagScout = null;
        }

        if (first.spd > second.spd) {
            first.flagSpd = true;
            second.flagSpd = false;
        } else if (first.spd < second.spd) {
            first.flagSpd = false;
            second.flagSpd = true;
        } else {
            first.flagSpd = null;
            second.flagSpd = null;
        }

        if (first.oilConsume > second.oilConsume) {
            first.flagOil = true;
            second.flagOil = false;
        } else if (first.oilConsume < second.oilConsume) {
            first.flagOil = false;
            second.flagOil = true;
        } else {
            first.flagOil = null;
            second.flagOil = null;
        }

        if (first.ammoConsume > second.ammoConsume) {
            first.flagAmmo = true;
            second.flagAmmo = false;
        } else if (first.ammoConsume < second.ammoConsume) {
            first.flagAmmo = false;
            second.flagAmmo = true;
        } else {
            first.flagAmmo = null;
            second.flagAmmo = null;
        }
    }

    private void compare(AVGCompareItem first, AVGCompareItem second,
                         AVGCompareItem third) {
        AVGCompareItem[] tempArr = new AVGCompareItem[3];
        tempArr[0] = first;
        if(second.hp < tempArr[0].hp) {
            tempArr[1] = first;
            tempArr[0] = second;
        } else {
            tempArr[1] = second;
        }
        if(third.hp < tempArr[0].hp) {
            tempArr[2] = tempArr[1];
            tempArr[1] = tempArr[0];
            tempArr[0] = third;
        } else if(third.hp >= tempArr[0].hp && third.hp < tempArr[1].hp) {
            tempArr[2] = tempArr[1];
            tempArr[1] = third;
        } else {
            tempArr[2] = third;
        }
        if(tempArr[0].hp.equals(tempArr[1].hp) && tempArr[1].hp.equals(tempArr[2].hp)) {
            tempArr[0].flagHP = null;
            tempArr[1].flagHP = null;
            tempArr[2].flagHP = null;
        } else {
            if(tempArr[0].hp.equals(tempArr[1].hp)) {
                tempArr[0].flagHP = null;
                tempArr[1].flagHP = null;
                tempArr[2].flagHP = true;
            } else if(tempArr[1].hp.equals(tempArr[2].hp)) {
                tempArr[0].flagHP = false;
                tempArr[1].flagHP = null;
                tempArr[2].flagHP = null;
            } else {
                tempArr[0].flagHP = false;
                tempArr[1].flagHP = null;
                tempArr[2].flagHP = true;
            }
        }

        tempArr[0] = first;
        if(second.armor < tempArr[0].armor) {
            tempArr[1] = first;
            tempArr[0] = second;
        } else {
            tempArr[1] = second;
        }
        if(third.armor < tempArr[0].armor) {
            tempArr[2] = tempArr[1];
            tempArr[1] = tempArr[0];
            tempArr[0] = third;
        } else if(third.armor >= tempArr[0].armor && third.armor < tempArr[1].armor) {
            tempArr[2] = tempArr[1];
            tempArr[1] = third;
        } else {
            tempArr[2] = third;
        }
        if(tempArr[0].armor.equals(tempArr[1].armor) && tempArr[1].armor.equals(tempArr[2].armor)) {
            tempArr[0].flagAmor = null;
            tempArr[1].flagAmor = null;
            tempArr[2].flagAmor = null;
        } else {
            if(tempArr[0].armor.equals(tempArr[1].armor)) {
                tempArr[0].flagAmor = null;
                tempArr[1].flagAmor = null;
                tempArr[2].flagAmor = true;
            } else if(tempArr[1].armor.equals(tempArr[2].armor)) {
                tempArr[0].flagAmor = false;
                tempArr[1].flagAmor = null;
                tempArr[2].flagAmor = null;
            } else {
                tempArr[0].flagAmor = false;
                tempArr[1].flagAmor = null;
                tempArr[2].flagAmor = true;
            }
        }

        tempArr[0] = first;
        if(second.shield < tempArr[0].shield) {
            tempArr[1] = first;
            tempArr[0] = second;
        } else {
            tempArr[1] = second;
        }
        if(third.shield < tempArr[0].shield) {
            tempArr[2] = tempArr[1];
            tempArr[1] = tempArr[0];
            tempArr[0] = third;
        } else if(third.shield >= tempArr[0].shield && third.shield < tempArr[1].shield) {
            tempArr[2] = tempArr[1];
            tempArr[1] = third;
        } else {
            tempArr[2] = third;
        }
        if(tempArr[0].shield.equals(tempArr[1].shield) && tempArr[1].shield.equals(tempArr[2].shield)) {
            tempArr[0].flagShield = null;
            tempArr[1].flagShield = null;
            tempArr[2].flagShield = null;
        } else {
            if(tempArr[0].shield.equals(tempArr[1].shield)) {
                tempArr[0].flagShield = null;
                tempArr[1].flagShield = null;
                tempArr[2].flagShield = true;
            } else if(tempArr[1].shield.equals(tempArr[2].shield)) {
                tempArr[0].flagShield = false;
                tempArr[1].flagShield = null;
                tempArr[2].flagShield = null;
            } else {
                tempArr[0].flagShield = false;
                tempArr[1].flagShield = null;
                tempArr[2].flagShield = true;
            }
        }

        tempArr[0] = first;
        if(second.resist < tempArr[0].resist) {
            tempArr[1] = first;
            tempArr[0] = second;
        } else {
            tempArr[1] = second;
        }
        if(third.resist < tempArr[0].resist) {
            tempArr[2] = tempArr[1];
            tempArr[1] = tempArr[0];
            tempArr[0] = third;
        } else if(third.resist >= tempArr[0].resist && third.resist < tempArr[1].resist) {
            tempArr[2] = tempArr[1];
            tempArr[1] = third;
        } else {
            tempArr[2] = third;
        }
        if(tempArr[0].resist.equals(tempArr[1].resist) && tempArr[1].resist.equals(tempArr[2].resist)) {
            tempArr[0].flagResist = null;
            tempArr[1].flagResist = null;
            tempArr[2].flagResist = null;
        } else {
            if(tempArr[0].resist.equals(tempArr[1].resist)) {
                tempArr[0].flagResist = null;
                tempArr[1].flagResist = null;
                tempArr[2].flagResist = true;
            } else if(tempArr[1].resist.equals(tempArr[2].resist)) {
                tempArr[0].flagResist = false;
                tempArr[1].flagResist = null;
                tempArr[2].flagResist = null;
            } else {
                tempArr[0].flagResist = false;
                tempArr[1].flagResist = null;
                tempArr[2].flagResist = true;
            }
        }

        tempArr[0] = first;
        if(second.atk < tempArr[0].atk) {
            tempArr[1] = first;
            tempArr[0] = second;
        } else {
            tempArr[1] = second;
        }
        if(third.atk < tempArr[0].atk) {
            tempArr[2] = tempArr[1];
            tempArr[1] = tempArr[0];
            tempArr[0] = third;
        } else if(third.atk >= tempArr[0].atk && third.atk < tempArr[1].atk) {
            tempArr[2] = tempArr[1];
            tempArr[1] = third;
        } else {
            tempArr[2] = third;
        }
        if(tempArr[0].atk.equals(tempArr[1].atk) && tempArr[1].atk.equals(tempArr[2].atk)) {
            tempArr[0].flagAtk = null;
            tempArr[1].flagAtk = null;
            tempArr[2].flagAtk = null;
        } else {
            if(tempArr[0].atk.equals(tempArr[1].atk)) {
                tempArr[0].flagAtk = null;
                tempArr[1].flagAtk = null;
                tempArr[2].flagAtk = true;
            } else if(tempArr[1].atk.equals(tempArr[2].atk)) {
                tempArr[0].flagAtk = false;
                tempArr[1].flagAtk = null;
                tempArr[2].flagAtk = null;
            } else {
                tempArr[0].flagAtk = false;
                tempArr[1].flagAtk = null;
                tempArr[2].flagAtk = true;
            }
        }

        tempArr[0] = first;
        if(second.scout < tempArr[0].scout) {
            tempArr[1] = first;
            tempArr[0] = second;
        } else {
            tempArr[1] = second;
        }
        if(third.scout < tempArr[0].scout) {
            tempArr[2] = tempArr[1];
            tempArr[1] = tempArr[0];
            tempArr[0] = third;
        } else if(third.scout >= tempArr[0].scout && third.scout < tempArr[1].scout) {
            tempArr[2] = tempArr[1];
            tempArr[1] = third;
        } else {
            tempArr[2] = third;
        }
        if(tempArr[0].scout.equals(tempArr[1].scout) && tempArr[1].scout.equals(tempArr[2].scout)) {
            tempArr[0].flagScout = null;
            tempArr[1].flagScout = null;
            tempArr[2].flagScout = null;
        } else {
            if(tempArr[0].scout.equals(tempArr[1].scout)) {
                tempArr[0].flagScout = null;
                tempArr[1].flagScout = null;
                tempArr[2].flagScout = true;
            } else if(tempArr[1].scout.equals(tempArr[2].scout)) {
                tempArr[0].flagScout = false;
                tempArr[1].flagScout = null;
                tempArr[2].flagScout = null;
            } else {
                tempArr[0].flagScout = false;
                tempArr[1].flagScout = null;
                tempArr[2].flagScout = true;
            }
        }

        tempArr[0] = first;
        if(second.spd < tempArr[0].spd) {
            tempArr[1] = first;
            tempArr[0] = second;
        } else {
            tempArr[1] = second;
        }
        if(third.spd < tempArr[0].spd) {
            tempArr[2] = tempArr[1];
            tempArr[1] = tempArr[0];
            tempArr[0] = third;
        } else if(third.spd >= tempArr[0].spd && third.spd < tempArr[1].spd) {
            tempArr[2] = tempArr[1];
            tempArr[1] = third;
        } else {
            tempArr[2] = third;
        }
        if(tempArr[0].spd.equals(tempArr[1].spd) && tempArr[1].spd.equals(tempArr[2].spd)) {
            tempArr[0].flagSpd = null;
            tempArr[1].flagSpd = null;
            tempArr[2].flagSpd = null;
        } else {
            if(tempArr[0].spd.equals(tempArr[1].spd)) {
                tempArr[0].flagSpd = null;
                tempArr[1].flagSpd = null;
                tempArr[2].flagSpd = true;
            } else if(tempArr[1].spd.equals(tempArr[2].spd)) {
                tempArr[0].flagSpd = false;
                tempArr[1].flagSpd = null;
                tempArr[2].flagSpd = null;
            } else {
                tempArr[0].flagSpd = false;
                tempArr[1].flagSpd = null;
                tempArr[2].flagSpd = true;
            }
        }

        tempArr[0] = first;
        if(second.oilConsume < tempArr[0].oilConsume) {
            tempArr[1] = first;
            tempArr[0] = second;
        } else {
            tempArr[1] = second;
        }
        if(third.oilConsume < tempArr[0].oilConsume) {
            tempArr[2] = tempArr[1];
            tempArr[1] = tempArr[0];
            tempArr[0] = third;
        } else if(third.oilConsume >= tempArr[0].oilConsume && third.oilConsume < tempArr[1].oilConsume) {
            tempArr[2] = tempArr[1];
            tempArr[1] = third;
        } else {
            tempArr[2] = third;
        }
        if(tempArr[0].oilConsume.equals(tempArr[1].oilConsume) && tempArr[1].oilConsume.equals(tempArr[2].oilConsume)) {
            tempArr[0].flagOil = null;
            tempArr[1].flagOil = null;
            tempArr[2].flagOil = null;
        } else {
            if(tempArr[0].oilConsume.equals(tempArr[1].oilConsume)) {
                tempArr[0].flagOil = null;
                tempArr[1].flagOil = null;
                tempArr[2].flagOil = true;
            } else if(tempArr[1].oilConsume.equals(tempArr[2].oilConsume)) {
                tempArr[0].flagOil = false;
                tempArr[1].flagOil = null;
                tempArr[2].flagOil = null;
            } else {
                tempArr[0].flagOil = false;
                tempArr[1].flagOil = null;
                tempArr[2].flagOil = true;
            }
        }

        tempArr[0] = first;
        if(second.ammoConsume < tempArr[0].ammoConsume) {
            tempArr[1] = first;
            tempArr[0] = second;
        } else {
            tempArr[1] = second;
        }
        if(third.ammoConsume < tempArr[0].ammoConsume) {
            tempArr[2] = tempArr[1];
            tempArr[1] = tempArr[0];
            tempArr[0] = third;
        } else if(third.ammoConsume >= tempArr[0].ammoConsume && third.ammoConsume < tempArr[1].ammoConsume) {
            tempArr[2] = tempArr[1];
            tempArr[1] = third;
        } else {
            tempArr[2] = third;
        }
        if(tempArr[0].ammoConsume.equals(tempArr[1].ammoConsume) && tempArr[1].ammoConsume.equals(tempArr[2].ammoConsume)) {
            tempArr[0].flagAmmo = null;
            tempArr[1].flagAmmo = null;
            tempArr[2].flagAmmo = null;
        } else {
            if(tempArr[0].ammoConsume.equals(tempArr[1].ammoConsume)) {
                tempArr[0].flagAmmo = null;
                tempArr[1].flagAmmo = null;
                tempArr[2].flagAmmo = true;
            } else if(tempArr[1].ammoConsume.equals(tempArr[2].ammoConsume)) {
                tempArr[0].flagAmmo = false;
                tempArr[1].flagAmmo = null;
                tempArr[2].flagAmmo = null;
            } else {
                tempArr[0].flagAmmo = false;
                tempArr[1].flagAmmo = null;
                tempArr[2].flagAmmo = true;
            }
        }
    }

    @OnClick(R.id.btnRunCompPop)
    public void RunCompareClick() {
        String levelStr = editLevelCompPop.getText().toString();
        String enhanceStr = editEnhanceCompPop.getText().toString();
        try {
            if (levelStr.length() == 0) {
                Toast.makeText(resContext, "请输入等级...", Toast.LENGTH_LONG).show();
            } else {
                level = Integer.parseInt(levelStr);
                enhance = 0;
                if (enhanceStr.length() > 0) {
                    enhance = Integer.parseInt(enhanceStr);
                }
                if (level > 0 && level <= 65 && enhance >= 0 && enhance <= 65) {
                    updateViews();
                } else {
                    Toast.makeText(resContext, "请输入合适的数值...", Toast.LENGTH_LONG)
                            .show();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.btnCancelCompPop)
    public void CancelCompareClick() {
        dismiss();
    }

}
